package com.loto.insurance.oem.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.loto.insurance.oem.web.service.HomeService;
import com.loto.insurance.oem.web.service.PolicyInfoService;
import com.loto.insurance.oem.web.utils.Config;
import com.loto.insurance.oem.web.authority.OEMAuthority;
import com.loto.insurance.oem.web.entity.Pager;
import com.loto.insurance.oem.web.entity.SessionUser;
import com.loto.insurance.oem.web.entity.config.*;
import com.loto.insurance.oem.web.enumeration.ConfigFileEnum;
import com.loto.insurance.oem.web.enumeration.CustFromEnum;
import com.loto.insurance.oem.web.enumeration.MenuEnum;

@Controller
public class HomeController {
  @Autowired
  HomeService homeService;

  @Autowired
  PolicyInfoService policyInfoService;

  @OEMAuthority
  @RequestMapping(value = "/")
  public String home(Model model) {
	return "home";
  }

  @RequestMapping(value = "/reg")
  public String menu(Model model) {
	model.addAttribute("fromList", CustFromEnum.values());
	return "reg";
  }

  @RequestMapping(value = "/ctrl/menu")
  public String menu(
	  @RequestParam(value = "pageId", required = false, defaultValue = "0") int pageId,
	  HttpServletRequest request, Model model) {
	HttpSession session = request.getSession();
	SessionUser user = (SessionUser) session.getAttribute("oem_user");
	model.addAttribute("pageId", pageId);
	if (user != null) {
	  model.addAttribute("roleId", user.getRoleId());
	  model.addAttribute("menuList", MenuEnum.values());
	}
	return "ctrl/menu";
  }

  @RequestMapping(value = "/ctrl/top")
  public String top(HttpServletRequest request, Model model) {

	OEMConfig config = Config.getConfigFile(request, ConfigFileEnum.OEMCONFIG,
	    OEMConfig.class);

	if (config != null)
	  model.addAttribute("oemName", config.getOemName());

	HttpSession session = request.getSession();
	SessionUser user = (SessionUser) session.getAttribute("oem_user");
	if (user != null) {
	  model.addAttribute("loginName", user.getUsername());
	  long problemCount = policyInfoService.findProblemPoliciesByCustCount(user
		  .getUserId());
	  if (problemCount > 0)
		model.addAttribute("problemCount", "(" + problemCount + ")");
	}

	/*
	 * OEMConfig config = new OEMConfig(); OemPayAccount oemPayAccount = new
	 * OemPayAccount(); AlipayAccount alipayAccount = new AlipayAccount();
	 * ArrayList<PayAccount> alipay = new ArrayList<PayAccount>(); PayAccount
	 * account = new PayAccount(); account.setAcc("eeee@lequgo.com");
	 * account.setKey("c"); account.setSecret("cc"); alipay.add(account);
	 * account = new PayAccount(); account.setAcc("d@lequgo.com");
	 * account.setKey("d"); account.setSecret("dd"); alipay.add(account);
	 * alipayAccount.setAccount(alipay); oemPayAccount.setAlipay(alipayAccount);
	 * config.setOemPayAccount(oemPayAccount); config.setOemDomain("localhost");
	 * config.setOemICP("备案"); config.setOemName("hhgg"); String s =
	 * XML.toXML(config, OEMConfig.class); String fileName =
	 * request.getSession().getServletContext() .getRealPath("/") +
	 * ConfigFileEnum.OEMCONFIG.toString(); boolean isSuc =
	 * XML.toXMLFile(config, OEMConfig.class, fileName);
	 * 
	 * config1 = Config.getConfigFile(request, ConfigFileEnum.OEMCONFIG,
	 * OEMConfig.class);
	 * 
	 * if (config1 != null) model.addAttribute("oemName", config1.getOemName());
	 */
	return "ctrl/top";
  }

  @RequestMapping(value = "/ctrl/header")
  public String header() {
	return "ctrl/header";
  }

  @RequestMapping(value = "/ctrl/bottom")
  public String bottom() {
	return "ctrl/bottom";
  }

  @RequestMapping(value = "/ctrl/footer")
  public String footer() {
	return "ctrl/footer";
  }

  @RequestMapping(value = "/ctrl/pager")
  public String pager(
	  @RequestParam(value = "index", required = false) int pageIndex,
	  @RequestParam(value = "pagesize", required = false, defaultValue = "10") int pageSize,
	  @RequestParam(value = "rowcount", required = true) long rowCount,
	  @RequestParam(value = "pagename", required = true) String pageName,
	  @RequestParam(value = "params", required = false) String params,
	  Model model) {
	Pager pager = new Pager();
	if (pageIndex > 0)
	  pager.setIndex(pageIndex);
	if (pageSize > 0)
	  pager.setPageSize(pageSize);
	if (params != null)
	  pager.setParams(params);
	pager.setRowCount(rowCount);
	pager.setPageName(pageName);
	model.addAttribute("pager", pager);
	return "ctrl/pager";
  }

  @RequestMapping(value = "/login")
  public String login() {
	return "login";
  }

  @RequestMapping(value = "/logout")
  public String logout(HttpServletRequest request) {
	HttpSession session = request.getSession();
	session.removeAttribute("oem_user");
	return "login";
  }
}
